package cn.edu.neusoft.wuguangsheng.market.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.market.R;
import cn.edu.neusoft.wuguangsheng.market.adapter.CartTiaoMuAdapter;
import cn.edu.neusoft.wuguangsheng.market.bean.ShowCartBean;
import cn.edu.neusoft.wuguangsheng.market.listener.ListListener;
import cn.edu.neusoft.wuguangsheng.market.model.ShowCartModel;

/**
 * A simple {@link Fragment} subclass.
 */
public class CartFragment extends BaseFragment {
    RecyclerView recyclercart;
    private List<ShowCartBean> items;
    Boolean up=false;
    TextView flagcart;
    @Override
    void initView() {

        recyclercart=(RecyclerView) view.findViewById(R.id.recyclecart);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclercart.setLayoutManager(linearLayoutManager);
        String uid=getid();
        ShowCartModel showCartModel=new ShowCartModel();
        showCartModel.showCart(uid,listListener);
        flagcart=(TextView) view.findViewById(R.id.flagcart);
    }

    @Override
    void initEvent() {

    }

    @Override
    void initDate() {

    }



    public CartFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setLayout_file(R.layout.fragment_cart);
        //Log.v(TAG,"onCreateView");
        return super.onCreateView(inflater,container,savedInstanceState);
    }


    ListListener<ShowCartBean> listListener=new ListListener<ShowCartBean>() {
        @Override
        public void onResponse(List<ShowCartBean> list) {
            items=list;
            if(items.equals("")){
                //flagcart.setText("快去添加商品到购物车吧");
                Toast.makeText(getContext(),"快去添加商品到购物车吧",Toast.LENGTH_SHORT).show();
            }

            else{
            CartTiaoMuAdapter cartTiaoMuAdapter=new CartTiaoMuAdapter(getContext(),items,R.layout.itemcart);
            recyclercart.setAdapter(cartTiaoMuAdapter);
            recyclercart.setItemAnimator(new DefaultItemAnimator());
        }
        }

        @Override
        public void onFail(String msg) {
            flagcart.setText("快去添加商品到购物车吧");
            //Toast.makeText(getContext(),"购物车为空",Toast.LENGTH_SHORT).show();
        }

    };


    @Override
    public int getLayout_file() {
        return super.getLayout_file();
    }

    @Override
    public void setLayout_file(int layout_file) {
        super.setLayout_file(layout_file);
    }


    @Override
    public void onPause() {
        super.onPause();
        up=true;

    }

    @Override
    public void onResume() {
        super.onResume();
        if(up){
            initView();
            up=false;
        }
    }




}
